/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAL;

import DTO.dConfig;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author nvc11190
 */
public class Config {
    
    //Tạo file XML Config
    public void setConfig(dConfig d){
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        try {
            builder = builderFactory.newDocumentBuilder();
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
        Document doc = builder.newDocument();
        Element config = doc.createElement("config");
        Element host = doc.createElement("host");
        Element port = doc.createElement("port");
        Element user = doc.createElement("username");
        Element pass = doc.createElement("pass");
        Element database = doc.createElement("database");
        host.setTextContent(d.getHost());
        port.setTextContent(d.getPort());
        user.setTextContent(d.getUserName());
        pass.setTextContent(d.getPassWord());
        database.setTextContent(d.getDatabase());
        config.appendChild(host);
        config.appendChild(port);
        config.appendChild(user);
        config.appendChild(pass);
        config.appendChild(database);
        doc.appendChild(config);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer tran = null;
        try {
            tran = factory.newTransformer();
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            tran.transform(new DOMSource(doc), new StreamResult(new File("config.xml")));
        } catch (TransformerException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        
        
    }
     public dConfig GetConfig(){
        dConfig dCon=null;
           try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        Document doc = (Document) builder.newDocument();
        doc = (Document) builder.parse("config.xml");
        NodeList Configlist = doc.getElementsByTagName("config");
        Node config = (Node) Configlist.item(0);
        NodeList childNodes = config.getChildNodes();
        Node hostNode = (Node) childNodes.item(0);
        Node portNode = (Node) childNodes.item(1);
        Node userNameNode = (Node) childNodes.item(2);
        Node passNode = (Node) childNodes.item(3);
        Node databaseNode = (Node) childNodes.item(4);
        
        dCon = new dConfig(hostNode.getTextContent(), portNode.getTextContent(), userNameNode.getTextContent(),passNode.getTextContent() , databaseNode.getTextContent());
         } catch (SAXException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
           return dCon;
}

      
}
